package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Address;
import com.itheima.reggie.domain.Cart;
import com.itheima.reggie.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController         //@ResponseBody + @Controller
public class CartController {

    //依赖注入service层
    @Autowired
    private CartService cartService;

    /**
     * 添加购物车
     * Request URL: http://localhost:8081/cart/add
     * Request Method: POST
     */
    @PostMapping("/cart/add")
    public ResultInfo save(@RequestBody Cart cartParam){      //接收在请求体的参数
        //1.调用service层
        Cart cart = cartService.save(cartParam);
        //2.返回响应
        return ResultInfo.success(cart);
    }

    /**
     * 查看购物车
     * Request URL: http://localhost:8081/cart/list
     * Request Method: GET
     */
    @GetMapping("/cart/list")
    public ResultInfo findList(){
        //1.调用service层查询
        List<Cart> cartList = cartService.findList();
        //2.并返回响应
        return ResultInfo.success(cartList);
    }

    /**
     * 修改购物车
     * Request URL: http://localhost:8081/cart/sub
     * Request Method: POST
     */
    @PostMapping("/cart/sub")
    public ResultInfo updateByCommoditId(@RequestBody Map<String,Long> param){  //获取请求体中的参数
        //1.获取前端获取的参数
        Long setmealId = param.get("setmealId");
        Long dishId = param.get("dishId");
        //2.调用service层
        Cart cart = cartService.updateByCommoditId(setmealId,dishId);
        //3.返回响应
        return ResultInfo.success(cart);
    }

    /**
     * 清空购物车
     * Request URL: http://localhost:8081/cart/clean
     * Request Method: DELETE
     */
    @DeleteMapping("/cart/clean")
    public ResultInfo deleteByUserId(){
        //1.调用service层
        cartService.clean();
        //2.返回响应
        return ResultInfo.success();
    }


}
